package JAVA300.第十一章多线程;

/**
 * 阻塞状态sleep
 * 模拟网络延时
 */
public class thread_11_threadState_blocked_sleep {
    public static void main(String[] args) {
        //一份资源
        web12306 tic = new web12306();
        new Thread(tic,"thread1").start();
        new Thread(tic,"thread2").start();
        new Thread(tic,"thread3").start();
    }
}
 class web12306 implements Runnable {

    private int tickets = 100;

    @Override
    public void run() {

        while (true) {
            if (tickets < 0) {
                break;
            }

            //如果有网络延迟等存在并发线程安全的情况
            //模拟网络延迟
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName() + "--" + tickets--);//获取正在运行线程的名字
        }

    }
}
